package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tutk.p2p.ConnectionManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.adapter.WorkTempChooseListAdapter;
import cn.innosmart.aq.bean.WorkLogTemp;
import cn.innosmart.aq.util.SharedUtil;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/12/1.
 */
public class WorkLogTempChooseActivity extends BaseActivity {

    private final int EXIT = 0;
    private final int LOADINGDATA = 1;
    private Toolbar toolbar;
    private ListView lvTempChoose;
    private WorkTempChooseListAdapter workTempChooseListAdapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case LOADINGDATA:
                    String str = SharedUtil.getInstance().readTemp(ConnectionManager.getInstance().getCurrentConnectionEntity().getUid());
                    try {
                        final JSONObject jsonWorkLogs = new JSONObject(str);
                        JSONArray jsonArray = jsonWorkLogs.getJSONArray("column");
                        final ArrayList<WorkLogTemp> workLogs = new ArrayList<>();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            WorkLogTemp workLog = new WorkLogTemp(jsonArray.getJSONObject(i));
                            workLogs.add(workLog);
                        }
                        workLogs.add(0, new WorkLogTemp());
                        hideloadingDialog();
                        lvTempChoose.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                WorkLogTemp workLog = workLogs.get(position);
                                Intent intent = new Intent(WorkLogTempChooseActivity.this, WorkLogTempActivity.class);
                                intent.putExtra("worklog", workLog.toJSonObject().toString());
                                startActivityForResult(intent,1001);
                                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                                finish();
                            }
                        });
                        workTempChooseListAdapter = new WorkTempChooseListAdapter(WorkLogTempChooseActivity.this, workLogs);
                        lvTempChoose.setAdapter(workTempChooseListAdapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(WorkLogTempChooseActivity.this, WorkLogActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_log_temp_choose);
        setBar();
        initView();
        String worklog = SharedUtil.getInstance().readTemp(ConnectionManager.getInstance().getCurrentConnectionEntity().getUid());
        if (TextUtils.isEmpty(worklog)) {
            SharedUtil.getInstance().writeTemp("{\n" +
                    "  \"column\": [\n" +
                    "    {\n" +
                    "      \"column\": [\n" +
                    "        {\n" +
                    "          \"type\": \"DATE\",\n" +
                    "          \"name\": \"开始日期\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"名称\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"姓名\",\n" +
                    "          \"isRequired\": true\n" +
                    "        }\n" +
                    "      ],\n" +
                    "      \"icon\": \"ic_test\",\n" +
                    "      \"display\": [\n" +
                    "        3,\n" +
                    "        1,\n" +
                    "        2\n" +
                    "      ],\n" +
                    "      \"title\": \"喂食\",\n" +
                    "      \"description\":\"“这是喂食记录的模板。”\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"column\": [\n" +
                    "        {\n" +
                    "          \"type\": \"DATE\",\n" +
                    "          \"name\": \"开始日期\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"名称\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"姓名\",\n" +
                    "          \"isRequired\": true\n" +
                    "        }\n" +
                    "      ],\n" +
                    "      \"icon\": \"ic_test\",\n" +
                    "      \"display\": [\n" +
                    "        3,\n" +
                    "        1,\n" +
                    "        2\n" +
                    "      ],\n" +
                    "      \"title\": \"换水\",\n" +
                    "      \"description\":\"“这是换水记录的模板。”\"\n" +
                    "    },\n" +
                    "    {\n" +
                    "      \"column\": [\n" +
                    "        {\n" +
                    "          \"type\": \"DATE\",\n" +
                    "          \"name\": \"开始日期\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"名称\",\n" +
                    "          \"isRequired\": true\n" +
                    "        },\n" +
                    "        {\n" +
                    "          \"type\": \"TEXT\",\n" +
                    "          \"name\": \"姓名\",\n" +
                    "          \"isRequired\": true\n" +
                    "        }\n" +
                    "      ],\n" +
                    "      \"icon\": \"ic_test\",\n" +
                    "      \"display\": [\n" +
                    "        3,\n" +
                    "        1,\n" +
                    "        2\n" +
                    "      ],\n" +
                    "      \"title\": \"照明\",\n" +
                    "      \"description\":\"“这是照明记录的模板。”\"\n" +
                    "    }\n" +
                    "  ]\n" +
                    "}", ConnectionManager.getInstance().getCurrentConnectionEntity().getUid());
        }
        loadingData();
    }

    private void loadingData() {
        loadingDialog();
        mHandler.sendEmptyMessageDelayed(LOADINGDATA, 500);
    }

    private void initView() {
        lvTempChoose = (ListView) findViewById(R.id.lv_temp_choose);
    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        TextView tv = (TextView) toolbar.findViewById(R.id.tv_toolbar);
        tv.setText(R.string.activity_work_log_temp_choose_title);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        setSupportActionBar(toolbar);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WorkLogTempChooseActivity.this, WorkLogActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        toolbar.setNavigationIcon(null);
    }


}
